﻿
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using S3.WMDSLEditor.Xslt;
using System.Xml;

namespace S3.WMDSLEditor.Client.Forms
{
    public partial class FormNewView : Form
    {
        private readonly ViewClass _viewClass = new ViewClass();

        private readonly Dictionary<string, string> _viewDictionary = new Dictionary<string, string>();

        public FormNewView(IEnumerable<string> viewNames)
        {
            InitializeComponent();

            foreach (string viewName in viewNames)
            {
                _viewDictionary.Add(viewName, viewName);
            }
        }

        private void FormNewView_Load(object sender, System.EventArgs e)
        {
            ppgViewClass.SelectedObject = _viewClass;
        }

        private void ppgViewClass_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            switch (e.ChangedItem.Label)
            {
                case "QueryExpression":
                    _viewClass.QueryExpression = (ViewClassExpression)e.ChangedItem.Value;
                    break;
            }
        }
    }
   
    [DefaultProperty("ViewClassName")]
    internal class ViewClass
    {
        private string _name = "ViewClass1";
        private string _description = string.Empty;
        private bool _generateDoubleDerived = true;
        private string _dbView = string.Empty;
        private string _displayName = string.Empty;
        private bool _useNullable = false;

        private string _propertyName = "dynqueryclass";
        private bool _propertyValue = true;

        private string _queryDescription = string.Empty;
        private string _queryDisplayName = string.Empty;
        private string _queryKind = "SQL";
        private ViewClassExpression _queryExpression = new ViewClassExpression();
        private string _queryName = "FindByDynamicParams";

        private string _queryPropertyValue = "dynquery";
        private string _queryPropertyName = "stereotype";

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("The name of the database view."), DefaultValue("")]
        public string DBView
        {
            get { return _dbView; }
            set { _dbView = value; }
        }

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("Used in the code generation of the generated model."), DefaultValue("")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("The display name."), DefaultValue("")]
        public string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("Name of the element (View Class)."), DefaultValue("ViewClass1")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("If true nullable value types is used for the attributes."), DefaultValue(false)]
        public bool UseNullable
        {
            get { return _useNullable; }
            set { _useNullable = value; }
        }

        [CategoryAttribute("ViewClass Attributes"), DescriptionAttribute("Generate double derived."), DefaultValue(true), ReadOnlyAttribute(true)]
        public bool GenerateDoubleDerived
        {
            get { return _generateDoubleDerived; }
            set { _generateDoubleDerived = value; }
        }


        [CategoryAttribute("ViewClass Properties"), DescriptionAttribute("View class property value"), DefaultValue(true)]
        public bool PropertyValue
        {
            get { return _propertyValue; }
            set { _propertyValue = value; }
        }

        [CategoryAttribute("ViewClass Properties"), DescriptionAttribute("View class property name"), DefaultValue("dynqueryclass")]
        public string PropertyName 
        {
            get { return _propertyName; }
            set { _propertyName = value; }
        }



        [CategoryAttribute("ViewQuery Attributes"), DescriptionAttribute("Used in the code generation of the generated model."), DefaultValue("")]
        public string QueryDescription
        {
            get { return _queryDescription; }
            set { _queryDescription = value; }
        }

        [CategoryAttribute("ViewQuery Attributes"), DescriptionAttribute("The display name"), DefaultValue("")]
        public string QueryDisplayName
        {
            get { return _queryDisplayName; }
            set { _queryDisplayName = value; }
        }

        [CategoryAttribute("ViewQuery Attributes"), DescriptionAttribute("SQL expression"), DefaultValue("Add (Dynamic SQL)")]
        public ViewClassExpression QueryExpression
        {
            get { return _queryExpression; }
            set { _queryExpression = value; }
        }

        [CategoryAttribute("ViewQuery Attributes"), DescriptionAttribute("Kind of query"),
        TypeConverter(typeof(NewViewConverter)), DefaultValue("SQL")]
        public string QueryKind
        {
            get { return _queryKind; }
            set { _queryKind = value; }
        }

        [CategoryAttribute("ViewQuery Attributes"), DescriptionAttribute("Name of the element query "), DefaultValue("FindByDynamicParams")]
        public string QueryName
        {
            get { return _queryName; }
            set { _queryName = value; }
        }


        [CategoryAttribute("ViewQuery Properties"), DescriptionAttribute("Query property value"), DefaultValue("dynquery")]
        public string QueryPropertyValue
        {
            get { return _queryPropertyValue; }
            set { _queryPropertyValue = value; }
        }

        [CategoryAttribute("ViewQuery Properties"), DescriptionAttribute("Query property name"), DefaultValue("stereotype")]
        public string QueryPropertyName
        {
            get { return _queryPropertyName; }
            set { _queryPropertyName = value; }
        }
    }

    [Editor(typeof(QueryEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    internal class ViewClassExpression
    {
        private string _sql;

        public string Sql
        {
            get { return _sql; }
            set { _sql = value; }
        }

        public override string ToString()
        {
            if(string.IsNullOrEmpty(_sql))
                return "Add (Dynamic SQL)";
            
            return _sql;
        }
    }

    internal class QueryEditor : UITypeEditor
    {    
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }    
        
        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)    
        {
            IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;        
            ViewClassExpression viewSql = value as ViewClassExpression;        
            if (svc != null && viewSql != null)        
            {                        
                using (FormNewViewSql form = new FormNewViewSql())            
                {                
                    form.Value = viewSql.Sql;
                
                    if (svc.ShowDialog(form) == DialogResult.OK)                
                    {                    
                        viewSql.Sql = form.Value;                
                    }            
                }        
            }
            return viewSql;    
        }
    }

    internal class NewViewConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new[] { "SQL", "OPath", "StoredProcedure" });
        }
    }
}
